home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Atari Compendium
/
The Atari Compendium (Toad Computers) (1994).iso
/
files
/
umich
/
utils
/
whati~gz.zoo
/
whatisin.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-13
|
7KB
|
360 lines
#undef DEBUG
#undef CHECK_MAGIC
/*
* whatisin - show list of documentation sections
*
* whatisin [-P path] [section]
*
* reads both $MANPATH/whatisin and $MANPATH/whatis._?_ files
*
* bugs:
* - need more error checking (of data in database)
* - perhaps a "magic number" for the whatis database? it is
* written, but #ifdef CHECK_MAGIC.
*
* todo:
* - invoke PAGER (or MANPAGER) on list if sections specified
* - allow continuation lines in whatis database:
*
* name\
* %1\
* %...
*/
static char *rcsid_whatisin_c = "$Id: whatisin.c,v 2.0 1992/09/13 05:02:44 rosenkra Exp $";
/*
* $Log: whatisin.c,v $
* Revision 2.0 1992/09/13 05:02:44 rosenkra
* total rewrite. this if first rev of this file.
*
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "whatis.h"
/*
* we only need one record here. we read a record and check all the
* command line args against it for a match
*/
struct rec r;
char *libpath = MANPATH;
int debugging = 0; /* for -d */
/*
* fcn prototypes
*/
void whatisin (int, char **);
void contents (int);
int parse_record (char *, struct rec *);
void print_record (int, struct rec *);
void usage (int);
#ifdef CHECK_MAGIC
int check_magic (void);
#endif
/*------------------------------*/
/* main */
/*------------------------------*/
void main (int argc, char *argv[])
{
char *lpath;
/*
* see if there is MANPATH in env. use it over default...
*/
if ((lpath = getenv ("MANPATH")) != (char *) NULL)
libpath = lpath;
else if ((lpath = getenv ("MANDIR")) != (char *) NULL)
libpath = lpath;
#ifdef DEBUG
else
fprintf (stderr, "whatisin: environment variable MANPATH not set, using default\n");
#endif
/*
* parse args
*/
for (argc--, argv++; argc && **argv == '-'; argc--, argv++)
{
switch (*(*argv+1))
{
case 'P': /* path for db */
case 'M':
argc--, argv++;
if (argc < 1)
usage (1);
libpath = *argv;
break;
case 'd': /* debug mode */
debugging = 1;
break;
case 'h': /* help */
usage (0);
/* NOTREACHED */
}
}
/*
* do it. if no args, print file containing description of sections.
* else print contents of sections in argv list
*/
if (argc < 1)
whatisin (argc, argv);
else
while (argc)
whatisin (argc--, argv++);
exit (0);
}
/*------------------------------*/
/* whatisin */
/*------------------------------*/
void whatisin (int argc, char **argv)
{
char dbname[256];
char buf[REC_SIZE];
int doall = 0;
int sect;
int foundit = 0;
/*
* if no args specified, we only want a description of each section.
* otherwise, set sect to that specified in argv. should be single
* char (0-9,l,n,o, etc).
*/
if (argc == 0)
doall++;
else
{
doall = 0;
sect = **argv;
}
/*
* set up name of whatisin database and open it
*/
sprintf (dbname, "%s%s%s.___", libpath, SLASH, WHATISIN);
if (freopen (dbname, "r", stdin) == (FILE *) NULL)
{
fprintf (stderr,
"whatisin: could not access file %s\n", dbname);
exit (1);
}
if (doall)
{
/*
* just copy whatisin file. skip lines starting with #
*/
while (1)
{
fgets (buf, REC_SIZE-1, stdin);
if (feof (stdin))
break;
if (buf[0] == '#')
continue;
fputs (buf, stdout);
}
}
else
{
/*
* look for line describing section
*/
while (1)
{
fgets (buf, REC_SIZE-1, stdin);
if (feof (stdin))
break;
if (buf[0] == sect)
{
foundit++;
break;
}
}
if (foundit)
{
/*
* print header info then get print all the
* things in that section. contents will reopen
* stdin as the particular whatis database.
*/
printf ("Section Description\n");
printf ("------- -----------\n");
printf ("%s\n\n", buf);
printf ("Contents\n");
printf ("--------\n");
contents (sect);
printf ("\n");
}
else
{
fprintf (stderr, "whatisin: no section %c\n", sect);
exit (1);
}
}
return;
}
/*------------------------------*/
/* contents */
/*------------------------------*/
void contents (int sect)
{
char dbname[256];
char buf[REC_SIZE];
/*
* set up db name. section is really a single ascii char, not an
* int. this is so we can have whatis for local, new, etc.
*/
if (sect == -1)
/* orig behavior (just single "whatis" file, never used here) */
sprintf (dbname, "%s%s%s", libpath, SLASH, WHATIS);
else
/* new: whatis._[0-9lno]_ */
sprintf (dbname, "%s%s%s._%c_", libpath, SLASH, WHATIS, sect);
/*
* reopen stdin as this file...
*/
if (debugging)
fprintf (stderr, "checking database file %s...\n", dbname);
if (freopen (dbname, "r", stdin) == (FILE *) NULL)
{
fprintf (stderr,
"whatisin: could not access file %s\n", dbname);
exit (1);
}
#ifdef CHECK_MAGIC
/*
* check file's magic
*/
if (check_magic ())
{
fprintf (stderr,
"whatisin: magic number is wrong for file %s\n",
dbname);
exit (1);
}
#endif
/*
* read file and print lines
*/
while (1)
{
/*
* get raw record from file
*/
fgets (buf, REC_SIZE-1, stdin);
if (feof (stdin))
break;
if (debugging)
fprintf (stderr, "%s", buf);
/*
* skip comment or blank lines
*/
if (buf[0] == '#' || buf[0] == '\0' || buf[0] == '\n')
continue;
/*
* parse the record and store in r
*/
parse_record (buf, &r);
if (debugging)
{
fprintf (stderr, "name: %s\n", r.name ? r.name : "(NULL)");
fprintf (stderr, "section: %s\n", r.section ? r.section : "(NULL)");
fprintf (stderr, "subsect: %s\n", r.subsect ? r.subsect : "(NULL)");
fprintf (stderr, "desc: %s\n", r.desc ? r.desc : "(NULL)");
}
/*
* print record (short form)
*/
print_record (0, &r);
}
return;
}
/*------------------------------*/
/* usage */
/*------------------------------*/
void usage (int excode)
{
#define U(x) fprintf(stderr,x);
U("usage: whatisin [-P path] [section ...]\n");
U(" -P path alternative path to databases (MANPATH)\n");
U(" section valid name of a manual section (single char). to get a\n");
U(" list, invoke whatisin with no args.\n");
exit (excode);
#undef U
}